package com.cicro.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cicro.domain.Girl;
import com.cicro.domain.Result;
import com.cicro.repository.GirlRepository;
import com.cicro.service.GirlService;
import com.cicro.utils.ResultUtil;

@RestController
public class GirlController {
	
	@Autowired
	private GirlRepository girlRepository;
	
	@Autowired
	private GirlService girlService;
	
	/**
	 * 查询所有女生列表
	 * @return
	 */
	@GetMapping(value="/girls")
	public List<Girl> girlList(){
		return girlRepository.findAll();
	}
	
	/**
	 * 添加一个女生
	 * @param cupSize
	 * @param age
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@PostMapping(value="girls")
	public Result<Girl> girlAdd(@Valid Girl girl, BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			return ResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
		}
		
		girl.setCupSize(girl.getCupSize());
		girl.setAge(girl.getAge());
		
		return ResultUtil.success(girlRepository.save(girl));
	} 
	
	/**
	 * 查询一个女生
	 * @param id
	 * @return
	 */
	@GetMapping(value="girls/{id}")
	public Girl girlFindOne(@PathVariable("id") Integer id){
		return girlRepository.findOne(id);
	}
	
	/**
	 * 更新女生信息
	 * @param id
	 * @param cupSize
	 * @param age
	 * @return
	 */
	@PutMapping(value="girls/{id}")
	public Girl girlUpdate(	@PathVariable("id") Integer id,
							@RequestParam("cupSize") String cupSize,
							@RequestParam("age") Integer age){
		Girl girl = new Girl();
		girl.setId(id);
		girl.setCupSize(cupSize);
		girl.setAge(age);
		return girlRepository.save(girl);
	}
	
	/**
	 * 删除一个女生
	 * @param id
	 */
	@DeleteMapping(value="girls/{id}")
	public void girlDelete(@PathVariable("id") Integer id){
		girlRepository.delete(id);
	}
	
	/**
	 * 通过年龄来查询女生列表
	 * @param age
	 * @return
	 */
	@GetMapping(value="girls/age/{age}")
	public List<Girl> girlListByAge(@PathVariable("age") Integer age){
		return girlRepository.findByAge(age);
	}
	
	@PostMapping(value="girls/two")
	public void girlTwo(){
		girlService.insertTwo();
	}
	
	@GetMapping(value="girls/getAge/{id}")
	public void getAge(@PathVariable("id") Integer id) throws Exception{
		System.out.println(id);
		girlService.getAge(id);
	}
}
